/**
 * 数据缓存处理的基类
 */
import {axiosPost} from './BaseAxios';
import {BaseModel} from './BaseModel';
import {BaseResult} from './BaseResult';

export default class BaseLayerModule extends BaseModel {
	/**
	 * 统一处理数据缓存以及判断回调层
	 * @param thyUrl 接口url
	 * @param thyParams 接口参数  let params = {}; params.area_type = area_type; 没有为null
	 * @param callBack 接口回调
	 * @param errorBack
	 */
	thyPostModuleData(thyUrl, thyParams, callBack, errorBack = function () {
	}) {
        console.log(process.env)
		thyUrl = process.env.VUE_APP_API_PATH + thyUrl;
        console.log(thyUrl)
		if (callBack === null || callBack === undefined) {
			return this._thyAxiosPost('post', thyUrl, thyParams);
		} else if (typeof (callBack) === 'function') {
			this._thyAxiosPost('post', thyUrl, thyParams, callBack, errorBack);
		} else {
			this.moduleError(callBack)
		}

	}
	/**
	 * 统一处理数据缓存以及判断回调层
	 * @param thyUrl 接口url
	 * @param thyParams 接口参数  let params = {}; params.area_type = area_type; 没有为null
	 * @param callBack 接口回调
	 * @param errorBack
	 */
	thyPostDataModuleData(thyUrl, thyParams, callBack, errorBack = function () {
	}) {
		thyUrl = process.env.VUE_APP_DATA_CENTER_API_PATH + thyUrl;
		console.log(thyUrl)
		if (callBack === null || callBack === undefined) {
			return this._thyAxiosPost('post', thyUrl, thyParams);
		} else if (typeof (callBack) === 'function') {
			this._thyAxiosPost('post', thyUrl, thyParams, callBack, errorBack);
		} else {
			this.moduleError(callBack)
		}

	}

	/**
	 * 统一处理数据缓存以及判断回调层 请求格式 FormData
	 * @returns
	 */
	thyPostModuleFormData(thyUrl, thyParams, callBack, errorBack = function () {
	}) {
		thyUrl = process.env.VUE_APP_API_PATH + thyUrl;
		if (callBack === null || callBack === undefined) {
			return this._thyAxiosPost('FormData', thyUrl, thyParams);
		} else if (typeof (callBack) === 'function') {
			this._thyAxiosPost('FormData', thyUrl, thyParams, callBack, errorBack);
		} else {
			this.moduleError(callBack)
		}

	}

	/**
	 * 封装的get请求
	 * @returns
	 */
	thyGetModuleData(thyUrl, thyParams, callBack, errorBack = function () {
	}) {
		thyUrl = process.env.VUE_APP_API_PATH + thyUrl;
		if (callBack === null || callBack === undefined) {
			return this._thyAxiosPost('get', thyUrl, thyParams);
		} else if (typeof (callBack) === 'function') {
			this._thyAxiosPost('get', thyUrl, thyParams, callBack, errorBack);
		} else {
			this.moduleError(callBack)
		}
	}

	/**
	 * python路径请求
	 * @param {*} thyUrl
	 * @param {*} thyParams
	 * @param {*} callBack
	 * @param {*} errorBack
	 * @returns
	 */
	thyGetPythonData(thyUrl, thyParams, callBack, errorBack = function () {
	}) {
		thyUrl = process.env.VUE_APP_STATIC_BASE_P_MIDDLE_URL + thyUrl;
		if (callBack === null || callBack === undefined) {
			this.errorDispose(new BaseResult(-1, '回调函数异常，请检查代码重试！'));
			return;
		}
		this._thyAxiosPost(thyUrl, thyParams, callBack, errorBack);
	}

	/**
	 * 友好的错误提醒
	 * @param {*} callBack
	 */
	moduleError(callBack) {
		if (typeof (callBack) === 'string' || typeof (callBack) === 'number') {
			this.errorDispose({
				code: -120,
				msg: thyUrl + '接口回调函数类似错误'
			}, null);
		} else {
			this.errorDispose({
				code: -120,
				msg: thyUrl + '接口回调函数错误'
			}, null);
		}
	}

	/**
	 * 如果需要错误的返回结果
	 * @param thyUrl
	 * @param thyParams
	 * @param callBack
	 * @param errorBack
	 * @param mode
	 * @private
	 */
	_thyAxiosPost(mode, thyUrl, thyParams, callBack, errorBack) {
		if (callBack) {
			axiosPost(mode, thyUrl, thyParams)
				.then(res => {
					callBack(res);
					// if(res.status == 2){
					// 	callBack(res.data);
					// }else{
					// 	this.errorDispose(res, errorBack);
					// }
				}, err => {
					this.errorDispose(err, errorBack);
				});
		} else {
			return axiosPost(mode, thyUrl, thyParams);
		}
	}
}
